﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Acrobat;

namespace TMS
{
    public partial class VehiclesControl : UserControl
    {
        private bool listOfVehiclesLoaded = false;
        public MySqlDataAdapter MyDA = new MySqlDataAdapter();
        //Categories cat = new Categories();
        //Product prod = new Product();
        //DataSet table1 = new DataSet();
        Users ActiveUser = Program.GetActiveUser;

        public VehiclesControl()
        {
            Program.GetMainForm.Text = "TMS - Возила";
            InitializeComponent();
            if (!listOfVehiclesLoaded)
            {
                loadListOfVehicles();
                listOfVehiclesLoaded = true;
            }
        }

        public VehiclesControl(DataGridViewRow parameters)
        {
            Program.GetMainForm.Text = "TMS - Возила";
            InitializeComponent();
            if (!listOfVehiclesLoaded)
            {
                loadListOfVehicles();
                listOfVehiclesLoaded = true;
            }

        }

        private void loadListOfVehicles()
        {
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection mysqlCon = new MySqlConnection(connString);
            mysqlCon.Open();


            string sqlSelectAll = "SELECT * FROM Vehicles";

            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);


            DataSet table = new DataSet();
            MyDA.Fill(table);

            dgvVehiclesList.DataSource = table.Tables[0];


            dgvVehiclesList.Columns[0].Visible = false;
            dgvVehiclesList.Columns[1].HeaderText = "Модел на возило";
            dgvVehiclesList.Columns[2].HeaderText = "Регистрација";
            dgvVehiclesList.Columns[3].HeaderText = "Возач";
            
        }

        private void dgvVehiclesList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (ActiveUser.EditVehicles == true)
            //{
                DataGridViewRow row = dgvVehiclesList.CurrentRow;
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_VEHICLES_CONTROL, row);
                Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_VEHICLES_CONTROL, row);
                this.Dispose();
            //}
            
        }

        private void toolStripNewVehicle_Click(object sender, EventArgs e)
        {
            //if (ActiveUser.EditVehicles == true)
            //{
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.VEHICLES_CONTROL, null);
                this.Dispose();
                Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_VEHICLES_CONTROL);
            //}
            //else
            //    MessageBox.Show("Немате доволно привилегии за додавање на Возило");
        }
        
        private void toolStripBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }

        private void toolStripForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }


    }
}
